//
//  LSZHistoricalRecordVC.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/19.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZHistoricalRecordVC.h"

@interface LSZHistoricalRecordVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewtableViewHistoricalRecord;
@property(nonatomic,strong) NSArray * arrayExisting;
@property(nonatomic,assign) NSInteger indexNum;
@end

@implementation LSZHistoricalRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"历史记录";
    LSZArticleDetailsModel * model = [[LSZArticleDetailsModel alloc]init];
    self.arrayExisting = [WHCSqlite query:[model class]];
    self.arrayExisting = (NSArray *)[[self.arrayExisting reverseObjectEnumerator]allObjects];//逆序
    [self setHistoricalRecordUi];

    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithTitle:@"全删" style:UIBarButtonItemStylePlain target:self action:@selector(clickDelete)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}
-(void)clickDelete
{
    LSZArticleDetailsModel * model = [[LSZArticleDetailsModel alloc]init];
     NSArray  * arrayExisting = [WHCSqlite query:[model class]];
    if(arrayExisting.count >0){
        [WHC_ModelSqlite removeModel:[LSZArticleDetailsModel class]];
        [self initWithtring:@"已经全部删除!"];
        self.arrayExisting = [WHCSqlite query:[model class]];
        self.arrayExisting = (NSArray *)[[self.arrayExisting reverseObjectEnumerator]allObjects];//逆序
        [self.tableViewtableViewHistoricalRecord reloadData];
    }else{
        [self initWithtring:@"现在已没有数据啦!"];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayExisting.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZDocumentdetailsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZDocumentdetailsCell" forIndexPath:indexPath];
    if (self.arrayExisting.count > indexPath.row) {
        LSZArticleDetailsModel * mg = nil;
        mg = self.arrayExisting[indexPath.row];
        cell.articleDetailsModel = mg;
        NSLog(@"%@",mg.url);
    }
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
    //设定最小的长按时间 按不够这个时间不响应手势
    longPressGR.minimumPressDuration = 0.7;
    [cell addGestureRecognizer:longPressGR];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -实现手势对应的功能
-(void)lpGR:(UILongPressGestureRecognizer *)lpGR
{
    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
        CGPoint point = [lpGR locationInView:self.tableViewtableViewHistoricalRecord];
        NSIndexPath *indexPath =  [self.tableViewtableViewHistoricalRecord indexPathForRowAtPoint:point];
        if (indexPath != nil){
            self.indexNum = indexPath.row;
        }
    }
    if (lpGR.state == UIGestureRecognizerStateEnded){
        //手势结束
        [self longPressDelete];//添加弹窗
    }
}
-(void)longPressDelete
{
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"删除"] actionSheetBlock:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            LSZArticleDetailsModel * model = [[LSZArticleDetailsModel alloc]init];
            model = self.arrayExisting[self.indexNum];
            NSString * url = model.url;
            [WHC_ModelSqlite delete:[LSZArticleDetailsModel class] where:[NSString stringWithFormat:@"url='%@'",url]];
            self.arrayExisting = [WHCSqlite query:[model class]];
            self.arrayExisting = (NSArray *)[[self.arrayExisting reverseObjectEnumerator]allObjects];//逆序
            [self.tableViewtableViewHistoricalRecord reloadData];
        }}];
    [actionSheet show];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.arrayExisting.count > indexPath.row) {
        LSZArticleDetailsModel * mg = nil;
        mg = self.arrayExisting[indexPath.row];
        LSZDirectoryDetailsViewController * vc = [[LSZDirectoryDetailsViewController alloc]init];
        vc.url = mg.url;
        vc.productID = mg.id_id;
        vc.imageViewStr = mg.imageViewStr;
        vc.titleStr = mg.titleStr;
        vc.publishTime = mg.publishTime;
        vc.period = @"-1";
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(void)setHistoricalRecordUi
{
    _tableViewtableViewHistoricalRecord = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    _tableViewtableViewHistoricalRecord.delegate = self;
    _tableViewtableViewHistoricalRecord.dataSource = self;
    _tableViewtableViewHistoricalRecord.estimatedRowHeight=0;
    _tableViewtableViewHistoricalRecord.estimatedSectionFooterHeight =0;
    _tableViewtableViewHistoricalRecord.estimatedSectionHeaderHeight =0;
    [_tableViewtableViewHistoricalRecord setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
    [self.view addSubview:_tableViewtableViewHistoricalRecord];
    [self.tableViewtableViewHistoricalRecord registerClass:[LSZDocumentdetailsCell class] forCellReuseIdentifier:@"LSZDocumentdetailsCell"];
}

@end
